package com.sisqorap.andlotto;

import java.util.Arrays;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

public class AndLottoActivity extends Activity {
	private int LARGESTNUMBER = 7;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        FrameLayout main = (FrameLayout) findViewById(R.id.frameView);
        Ball b1 = new Ball(this,50,50,25,"33");
        
        main.addView(b1);
        GenerateNumbersAndPutOnScreen(6);
    }
    
    public void RegenerateNumbers(View view){
    	GenerateNumbersAndPutOnScreen(6);
    }
    
    public int[] GiveNumbers(int[] numbers, int largestNumber){
		for(int i = 0; i<numbers.length; i++){
			numbers[i] = GiveRandomNumber(numbers, largestNumber);
			/*Random r = new Random();
			int number = r.nextInt(LARGESTNUMBER);
			if(number > 0 && !Arrays.asList(numbers).contains(number))
				numbers[i] = number;
			else{
				Log.v("GiveNumbers", "Enter else");
				numbers = GiveNumbers(numbers, i);
				break;
			}*/
		}
		return numbers;
    }
    
	//http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates
    private int GiveRandomNumber(int[] numbers, int largestNumber){
    	Random r = new Random();
		int number = r.nextInt(largestNumber);
		int counter = 0;
		while(true){
			counter++;
			Log.v("GiveRandomNumber", "Counter: " + counter);
			if(number >= 0){// || Arrays.asList(numbers).contains(number))
				boolean existsAlready = false;
				for(int numberInNumbers : numbers){
					if(numberInNumbers == number){
						existsAlready = true;
						break;
					}
				}
				if(existsAlready)
					number = r.nextInt(largestNumber);
				else
					break;
			}
			else
				number = r.nextInt(largestNumber);
		}
		return number;
    }
    
    private void GenerateNumbersAndPutOnScreen(int numberOfNumbers){
    	TextView tv = (TextView)findViewById(R.id.tvLottoNumbers);
    	tv.setText("");
    	for(int i = 0; i < 2; i++){
	    	int[] numbers = new int[6]; 
	        numbers = GiveNumbers(numbers, LARGESTNUMBER);
			Arrays.sort(numbers);
			String numbersResult = "";
			for(int number : numbers){
				numbersResult += number + ", ";
			}
			tv.append(numbersResult.substring(0, numbersResult.length()-2) + "\r\n");
    	}
    }
}